//
//  HJJobVC.m
//  project
//
//  Created by jc on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJJobVC.h"

@interface HJJobVC ()
@property (nonatomic,strong) NSArray <HJJobListModel *>* dataArray;
@property (nonatomic,strong) NSIndexPath * cellIndex;
@end
static NSString * const identifier = @"HJJobCell";
@implementation HJJobVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navTitle = @"选择职业";
    self.rightNavTitle = @"完成";
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    [self loadNetworkData];
}

-(void)configTableView {
    self.needHeaderRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.tableHeaderView = [self tableViewHeaderVew];
    self.tableView.tableHeaderView.height = 46;
    self.tableView.rowHeight = 40;
    
    NSNumber * row = [mUserDefaults objectForKey:@"row"];
    self.cellIndex = [NSIndexPath indexPathForRow:[row integerValue] ? [row integerValue] : 0 inSection:0];
}

-(UIView *)tableViewHeaderVew {
    UILabel * title =[UILabel new];
    title.text= @"选择职业，让大家更好的了解你";
    title.textAlignment = NSTextAlignmentCenter;
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor colorWithHex:0x999999];
    title.font = [UIFont systemFontOFSize13];
    title.frame = CGRectMake(0, 0, SCREEN_WIDTH, 46);
    
    return title;
}

-(void)loadNetworkData {
    NSString * url = [Api stringByAppendingString:VocationApi];
    [HJHttpTool get:url params:nil success:^(HJResponeds *result) {
        LKLog(@"%@",result.data);
        self.dataArray = [NSArray yy_modelArrayWithClass:[HJJobListModel class] json:result.data];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(id result) {
        [self.tableView endRefresh];
        [self showErrorHUDWithError:result];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJJobCell * jobListCell =[tableView cellForRowAtIndexPath:indexPath];
    if (!jobListCell) {
        jobListCell = [[HJJobCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    jobListCell.selectionStyle = UITableViewCellSelectionStyleNone;
    [jobListCell loadMore:self.dataArray[indexPath.row]];
    
    if (indexPath.row == self.cellIndex.row) {
        jobListCell.isHide = YES;
    }
    if (!jobListCell.isHide) {
        jobListCell.rightImage.hidden = YES;
    }else {
        jobListCell.rightImage.hidden = NO;
    }
    
    return jobListCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJJobCell * jobListCell = [tableView cellForRowAtIndexPath:indexPath];
    if (jobListCell.isHide) {
        return;
    }else {
        jobListCell.isHide = YES;
        jobListCell.rightImage.hidden = NO;
        [mUserDefaults setObject:@(indexPath.row) forKey:@"row"];
        [mUserDefaults synchronize];
        if (self.cellIndex) {
            HJJobCell * cell1 =  (HJJobCell *)[tableView cellForRowAtIndexPath:self.cellIndex];
            cell1.rightImage.hidden = YES;
            cell1.isHide = NO;
        }
    }
    self.cellIndex = indexPath;
}

-(void)didClickRightNavAction {
    if (self.dataArray.count==0) {
        [self showErrorHUDWithError:@"暂无职业,无法提交"];
        return;
    }
    @weakify(self)
    [HJUserManager updateUserModelWithParams:@{@"vocationId":self.dataArray[self.cellIndex.row].name_id,@"vocationName":self.dataArray[self.cellIndex.row].name} success:^{
        @strongify(self)
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
@end


@implementation HJJobListModel

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    return @{@"name_id":@"id"};
}
@end



@implementation HJJobCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UILabel * label = [UILabel new];
        label.textColor = [UIColor blackColor];
        self.name = label;
        label.font = [UIFont fontWithName:PingFangSC_Medium size:13];
        [self.contentView addSubview:label];
        
        UIImageView * rightImage = [UIImageView new];
        rightImage.image = setImage(@"selecteImage");
        self.rightImage = rightImage;
        [self.contentView addSubview:rightImage];
    }
    return self;
}

-(void)loadMore:(HJJobListModel *)model {
    self.name.text = model.name;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self)
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(self.contentView.width-100, 30));
    }];
    [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).with.inset(10);
        make.size.mas_equalTo(CGSizeMake(self.rightImage.image.size.width, self.rightImage.image.size.height));
    }];
}
@end
